from apps.merchant.sku.baseViews import *

class SkuViewSet(BaseSkuViewSet):

    def get_queryset(self):
        params = getRequestParams(self.request)
        _list = SkuDao().findAll().select_related('item', 'item__cate', 'item__activity')
        if not params.get('itemActivity'):
            _list = _list.filter(item__is_delete=False, item__status=1)
        return _list

    # 批量修改库存
    @action(methods=['post'], detail=False)
    def updateAllStock(self, request):
        params = getRequestParams(self.request)
        obj_ids = params.get('obj_ids')
        if type(obj_ids) != list:
            return Response(response_message('obj_ids应为数组类型'))
        stock = params.get('stock')
        if not stock:
            return Response(response_message('库存不能为空'))
        _skus = SkuDao().findAll().filter(id__in=obj_ids)
        _skus.update(stock=stock)
        return Response(response_success('操作成功'))

    # 批量修改销售价
    @action(methods=['post'], detail=False)
    def updateAllSellingPrice(self, request):
        params = getRequestParams(self.request)
        obj_ids = params.get('obj_ids')
        if type(obj_ids) != list:
            return Response(response_message('obj_ids应为数组类型'))
        sellingPrice = params.get('sellingPrice')
        if not sellingPrice:
            return Response(response_message('销售价不能为空'))
        _skus = SkuDao().findAll().filter(id__in=obj_ids)
        _skus.update(sellingPrice=sellingPrice)
        return Response(response_success('操作成功'))